package com.example.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: 葛浩杰
 * @Version: 1.0 2021/1/29
 */
public class HistogramView extends View {
    /**
     * 自定义view实现柱状图
     * 首先定义一个类实现View
     */

    //定义画笔
    private Paint mGreenPaint;
    private Paint mTextPaint;
    //定义上下文
    private Context mContext;
    //定义宽高
    private float width;
    private float height;
    private float mScale;
    //这个数组是高度的值
    private String[] y_title = {"6", "5", "4", "3", "2", "1", "0"};
    //分别为定义数据与数据源名称的集合
    private List<Long> mData;
    private List<String> mNames;
    private int index = 10;//最大值分成多少份

    public HistogramView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //给定义的画笔进行加工
        mContext = context;
        mGreenPaint = new Paint();
        mTextPaint = new Paint();

        mGreenPaint.setARGB(255, 0, 200, 149);
        mTextPaint.setARGB(255, 153, 153, 153);

        mGreenPaint.setStyle(Paint.Style.FILL);

        mTextPaint.setAntiAlias(true);
        mGreenPaint.setAntiAlias(true);

        mScale = context.getResources().getDisplayMetrics().density;
        //初始化数据
        mData = new ArrayList<>();
        mData.add(2l);
        mData.add(3l);
        mData.add(4l);
        mData.add(5l);
        mData.add(6l);
        mData.add(4l);
        mData.add(3l);
        mNames = new ArrayList<>();
        mNames.add("一");
        mNames.add("二");
        mNames.add("三");
        mNames.add("四");
        mNames.add("五");
        mNames.add("六");
        mNames.add("七");
    }

    //尺寸发生改变的时候调用
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
//        weight = 0.7F * w;
        height = 0.70F * h;
        width = w;
//        height = h;
    }
    //绘制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float min_height = height / 6;
        for (int i = 6; i >= 0; i--) {
            mTextPaint.setTextAlign(Paint.Align.LEFT);
            mTextPaint.setTextSize(10 * mScale);
            //绘制0的时候，X是最左侧，y是第一个数字距离上边距的距离+每一块的距离，此处是32
            canvas.drawText(y_title[i], 0, 32 * mScale + min_height * i, mTextPaint);
        }

        //宽度去掉Y轴文字的宽度，除以数组长度，得到每个柱子的宽度+两边的间隔的宽度
        float min_width = (width - 10 * mScale) / (mData.size());
        mTextPaint.setTextSize(12 * mScale);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        for (int i = 0; i < mData.size(); i++) {
            int leftR = (int) ( i * min_width + min_width / 2/2);//每一块的一半作为当前柱子的两边间隔
            int rightR = leftR + (int) (10 * mScale);
            int buttomR = (int) (32 * mScale + min_height * 6);//表格上空出部分 + 高度最小值*6块 = 底部位置
            int topR = buttomR - (int) (height / index * mData.get(i));
//            canvas.drawRect(new RectF(leftR, topR, rightR, buttomR), mGreenPaint);
            LinearGradient lg=new LinearGradient((rightR - leftR)/2,topR,(rightR - leftR)/2,buttomR, Color.RED,Color.BLUE, Shader.TileMode.MIRROR);
            mGreenPaint.setShader(lg);
            canvas.drawRoundRect(new RectF(leftR, topR, rightR, buttomR),50,50, mGreenPaint);
            mTextPaint.setARGB(255, 153, 153, 153);
            canvas.drawText(mNames.get(i), leftR + min_width / 4, buttomR + 20 * mScale, mTextPaint);
        }
    }
    //传入数据并进行绘制
    public void updateThisData(List<Long> data, List<String> name, int index) {
        mData = data;
        mNames = name;
        this.index = index;
        invalidate();
    }
}